package myJs.browse

import myJs.Implicits._
import myJs.api.Api
import myJs.cps.BootstrapTableCp.Column
import myJs.cps._
import myJs.myPkg._
import myJs.tool.Pojo._
import myJs.tool.{Pojo, Tool}
import slinky.core.FunctionalComponent
import slinky.core.annotations.react
import slinky.core.facade.Hooks._
import slinky.web.html._

import scala.collection.immutable.SeqMap
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSConverters._
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

/**
 * Created by yz on 21/1/2022
 */
@react object DatasetCp {

  type Props = Unit

  val component = FunctionalComponent[Props] { props =>

    val (datas, setDatas) = useState(List.empty[ujson.Obj])
    val (columns, setColumns) = useState(List.empty[Column])

    useEffect(() => {
      FTool.refreshTableB()
    }, List()
    )

    object FTool {

      def getTableRs = {
        val options = Tool.layerOptions.copy(shade = List("0.1", "#fff"))
        val index = layer.load(0, options)
        Api.getFileData("Browse_dataset.xlsx").map { response =>
          layer.close(index)
          response
        }
      }

      def refreshTableB = () => {
        getTableRs.map { response =>
          val curDatas = response.array
          val map = response.map
          val basicColumns = map.map { case (k, v) =>
            val searchKind = v match {
              case x if List("Species", "Sample Type", "Phenotypes").contains(x) => CheckboxSearch
              case _ => TextSearch
            }
            val title = v match {
              case "Phenotypes" => "Phenotype"
              case _ => v
            }
            val fmt = FTool.tbFmt(v)
            val comVF = FTool.comVFmt(v)
            val optionMap = searchKind match {
              case Pojo.CheckboxSearch => curDatas.flatMap { dict =>
                dict(k).split(";")
              }.distinct.optionMap
              case _ => SeqMap[String, String]()
            }
            Column(field = k, title = title, operable = true, searchKind = searchKind, formatter = fmt, comVF = comVF,
              optionMap = optionMap)
          }.toList
          setColumns(basicColumns)
          val datas = curDatas.map(x => writeJs(x).transform(reader[ujson.Obj]))
          setDatas(datas)
        }
      }

      def tbFmt(fileTitle: String) = (v: ujson.Value, row: ujson.Obj) => {
        fileTitle match {
          case "Source" =>
            v.str match {
              case "GTEx" => val curHref = "https://gtexportal.org/home/"
                a(target := "_blank", href := curHref, s"${v.str}")()
              case "E-MTAB-6863" => val curHref = "https://www.ebi.ac.uk/arrayexpress/experiments/E-MTAB-6863/"
                a(target := "_blank", href := curHref, s"${v.str}")()
              case "TCGA" => val curHref = "https://portal.gdc.cancer.gov/"
                a(target := "_blank", href := curHref, s"${v.str}")()
              case "Fudan-ICC" => val curHref = "https://pubmed.ncbi.nlm.nih.gov/34971568/"
                a(target := "_blank", href := curHref, s"${v.str}")()
              case "CCLE" => val curHref = "https://sites.broadinstitute.org/ccle/"
                a(target := "_blank", href := curHref, s"${v.str}")()
              case "Local_hbv" | "Local_PDX" => span(s"${v.str}")()
              case _ => val curHref = s"https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=${v.str}"
                a(target := "_blank", href := curHref, s"${v.str}")()
            }

          case _ => span(v.myStr)()
        }
      }

      def comVFmt(fileTitle: String) = (v: ujson.Value, row: ujson.Obj) => {
        fileTitle match {
          case _ => v.myStr
        }
      }

    }

    val exportDatas = List(
      ExportData("XLSX", href = s"${Api.apiUrl}/gepLiver/tool/downloadFile?fileName=Browse_dataset_use.xlsx")
    )

    div(
      div(style := Style(),
        BootstrapTableCp(columns = columns, datas = datas, pageList = List(10, 25, 50), pageSize = 10,
          selectColumnEnabled = false, allSearchTitles = List("Phenotype", "Sample Type", "Models"),
          exportData = exportDatas, showExport = true, search = true
        )
      ),
    )


  }

  object STool {

  }

}
